package coml.jiyun.hp.reviewtopic.ui.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import coml.jiyun.hp.reviewtopic.DbUtil;
import coml.jiyun.hp.reviewtopic.R;
import coml.jiyun.hp.reviewtopic.bean.HomeBeanTwo;
import coml.jiyun.hp.reviewtopic.db.HomeBeanTwoDao;
import coml.jiyun.hp.reviewtopic.ui.adapter.RlvCollectAdapter;
import coml.jiyun.hp.reviewtopic.util.ToastUtil;

import static coml.jiyun.hp.reviewtopic.base.BaseApp.getInstance;

/**
 * A simple {@link Fragment} subclass.
 */
public class CollectFragment extends Fragment  {

    private View view;
    private RecyclerView mCollectRlv;
    private ArrayList<HomeBeanTwo> homeBeanTwos;
    private RlvCollectAdapter rlvCollectAdapter;
    private LinearLayout mLl;
    private int mPos;
    private View inflate;
    private TextView viewById;
    private PopupWindow popupWindow;
    private List<HomeBeanTwo> query;

    public CollectFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_collect, container, false);
        initView(inflate);
        initQuery();
        return inflate;
    }

    private void initView(final View inflate){
        mCollectRlv = (RecyclerView) inflate.findViewById(R.id.collect_rlv);

        homeBeanTwos = new ArrayList<>();
        rlvCollectAdapter = new RlvCollectAdapter(getActivity(), homeBeanTwos);
        mCollectRlv.setAdapter(rlvCollectAdapter);
        mCollectRlv.setLayoutManager(new LinearLayoutManager(getActivity()));

        rlvCollectAdapter.setOnItemClick(new RlvCollectAdapter.onItemClick() {
            @Override
            public void dj(int i) {
                mPos = i;
                popwindow();
                deleteItem();
            }
        });
        mLl = (LinearLayout) inflate.findViewById(R.id.ll);
    }

    private void deleteItem() {
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                rlvCollectAdapter.delete(query.get(mPos));
//                rlvCollectAdapter.notifyDataSetChanged();
//                ToastUtil.showToastShort("删除成功");
//                popupWindow.dismiss();

                  HomeBeanTwo two = homeBeanTwos.get(mPos);
                          homeBeanTwos.remove(mPos);
                          rlvCollectAdapter.setHomeBeanTwos(homeBeanTwos);
                          DbUtil.dbUtil().delete(two);
                          rlvCollectAdapter.notifyDataSetChanged();
                          Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
                          popupWindow.dismiss();


            }
        });
    }

    private void popwindow() {
        inflate = LayoutInflater.from(getActivity()).inflate(R.layout.popwindow, null);
        popupWindow = new PopupWindow(inflate, GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.WRAP_CONTENT);
        viewById = inflate.findViewById(R.id.pop_delete);
        viewById.setText("删除");
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(mLl, Gravity.CENTER,0,0);
    }

    private void initQuery() {
        query = DbUtil.dbUtil().query();
        homeBeanTwos.addAll(query);
        rlvCollectAdapter.setHomeBeanTwos(homeBeanTwos);
        rlvCollectAdapter.notifyDataSetChanged();
}

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            initData();
        } else {
            if (homeBeanTwos != null && homeBeanTwos.size() > 0) {
                homeBeanTwos.clear();
            }
        }
    }

    private void initData() {
        HomeBeanTwoDao beanDao = getInstance().getDaoSession().getHomeBeanTwoDao();
        List<HomeBeanTwo> homeBeanTwos = beanDao.loadAll();
        this.homeBeanTwos.addAll(homeBeanTwos);
        rlvCollectAdapter.notifyDataSetChanged();
    }
}